package com.visualexpress.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import com.visualexpress.domain.Shipment;
import com.visualexpress.domain.User;
import com.visualexpress.domain.UserShipment;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserShipment entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.visualexpress.domain.UserShipment
 * @author MyEclipse Persistence Tools
 */

public class UserShipmentDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UserShipmentDAO.class);
	// property constants
	public static final String USER = "user";
	public static final String SHIPMENT = "shipment";
	public static final String DELIVERED = "delivered";
	public static final String CHANGED = "changed";

	public void save(UserShipment transientInstance) {
		log.debug("saving UserShipment instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().save(transientInstance);
			t.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(UserShipment persistentInstance) {
		log.debug("deleting UserShipment instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().delete(persistentInstance);
			t.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void deleteByUserAndShipment(String username,String shipmentNum) {
		log.debug("deleting UserShipment instance");
		UserDAO udao = new UserDAO();
		List listUser = (List)udao.findByUsername(username);
		if(listUser.isEmpty()){
			log.debug("delete failed,no user exist");
			return;
		}
		User user = (User)listUser.get(0);
		
		ShipmentDAO sdao = new ShipmentDAO();
		List listShipment = sdao.findByShipmentNum(shipmentNum);
		if(listShipment.isEmpty()){
			log.debug("delete failed,no shipment exist");
			return;
		}
		Shipment shipment = (Shipment)listShipment.get(0);
		Session session = null;
		try {
			List listUS = (List)this.findByShipmentAndUser(shipment, user);
			if(listUS.isEmpty()){
				log.debug("delete failed,no connection");
				return;
			}
			UserShipment userShipment = (UserShipment)listUS.get(0);
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			this.getSession().delete(userShipment);
			t.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			HibernateUtils.closeSession(session);
		}
	}

	public UserShipment findById(java.lang.Integer id) {
		log.debug("getting UserShipment instance with id: " + id);
		try {
			UserShipment instance = (UserShipment) getSession().get(
					"com.visualexpress.domain.UserShipment", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(UserShipment instance) {
		log.debug("finding UserShipment instance by example");
		try {
			List results = getSession().createCriteria(
					"com.visualexpress.domain.UserShipment").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserShipment instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from UserShipment as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByShipmentAndUser(Shipment shipment, User user) {
		log.debug("finding UserShipment instance with property  " + shipment +" and property"+user);
		try {
			String queryString = "from UserShipment as model where model.shipment= ? and model.user= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, shipment);
			queryObject.setParameter(1, user);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUser(Object user) {
		return findByProperty(USER, user);
	}

	public List findByShipment(Object shipment) {
		return findByProperty(SHIPMENT, shipment);
	}

	public List findByDelivered(Object delivered) {
		return findByProperty(DELIVERED, delivered);
	}

	public List findByChanged(Object changed) {
		return findByProperty(CHANGED, changed);
	}

	public List findAll() {
		log.debug("finding all UserShipment instances");
		try {
			String queryString = "from UserShipment";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public UserShipment merge(UserShipment detachedInstance) {
		log.debug("merging UserShipment instance");
		try {
			UserShipment result = (UserShipment) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(UserShipment instance) {
		log.debug("attaching dirty UserShipment instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().saveOrUpdate(instance);
			t.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(UserShipment instance) {
		log.debug("attaching clean UserShipment instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}